﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using TYBase;
using TYLitedbBase;

namespace EVEBatteryCheck.Data {
    public class LocalDBHelper {

        public LocalDBHelper(string saveDir) {
            SaveDir = saveDir;
        }
        LiteDB_BaseHelper<OrderData> dx_order = null;
        public bool InitDir() {
            var ret = DirectoryHelper.CheckDirectory(SaveDir);
            if (ret) {
                dx_order = new LiteDB_BaseHelper<OrderData>(Path.Combine(SaveDir, "data.db"), false);
            }
            return ret;
        }
        public string SaveDir { get; }

        private object obj = new object();
        public bool Save2DB(string orderNo, TrayData trayData, out string info) {
            info = null;
            try {
                lock (obj) {
                    string fileName = Path.Combine(SaveDir, $"{orderNo}.db");
                    var dx = new LiteDB_BaseHelper<TrayData>(fileName, false);
                    if (dx.Count(x => x.tray_code == trayData.tray_code) > 0) {
                        info = $"存在相同托盘 :{trayData.tray_code}";
                    }
                    dx.Insert(trayData);
                    return true;
                }
            }
            catch (Exception) {

                return false;
            }
        }

        public bool GetTrayData(string orderNo, string trayCode, out TrayData trayData) {
            trayData = null;
            if (orderNo.Null())
                return false;
            string fileName = Path.Combine(SaveDir, $"{orderNo}.db");
            var dx = new LiteDB_BaseHelper<TrayData>(fileName, false);
            return dx.Get(x => x.tray_code == trayCode, out trayData) && trayData != null;
        }

        public bool SaveOrderData(OrderData data) {
            if (dx_order == null)
                return false;

            return dx_order.Insert(data);
        }
        public bool SaveJumpTrayData(string orderNo, JumpTrayData data) {
            if (orderNo.Null())
                return false;
            string fileName = Path.Combine(SaveDir, $"{orderNo}.db");
            var dx = new LiteDB_BaseHelper<JumpTrayData>(fileName, false);
            return dx.Insert(data);
        }

        public bool SaveSameSeqData(string orderNo, SameSeqData data) {
            if (orderNo.Null())
                return false;
            string fileName = Path.Combine(SaveDir, $"{orderNo}.db");
            var dx = new LiteDB_BaseHelper<SameSeqData>(fileName, false);
            return dx.Insert(data);
        }

        public bool SaveSameSeqTrayData(string orderNo, SameSeqTrayData data) {
            if (orderNo.Null())
                return false;
            string fileName = Path.Combine(SaveDir, $"{orderNo}.db");
            var dx = new LiteDB_BaseHelper<SameSeqTrayData>(fileName, false);
            return dx.Insert(data);
        }
    }

    public class TrayData : Entity_LiteDB_Base {
        public string tray_code { get; set; }
        public bool is_same { get; set; }
        public bool is_jump { get; set; }
        public List<int> cells { get; set; }
        public DateTime create_time { get; set; }
        public bool iserror_at_robot { get; set; }
    }

    public class OrderData : Entity_LiteDB_Base {
        public string order_no { get; set; }
        public string order_no_ex { get; set; }
        public DateTime create_time { get; set; }
    }

    public class JumpTrayData : Entity_LiteDB_Base {
        public int seqNo { get; set; }
        public string tray_code { get; set; }
        public DateTime create_time { get; set; }
    }

    public class SameSeqData : Entity_LiteDB_Base {
        public int seqNo { get; set; }
        public int level { get; set; }
        public DateTime create_time { get; set; }
    }

    public class SameSeqTrayData : Entity_LiteDB_Base {
        public int seqNo { get; set; }
        public int level { get; set; }
        public string tray_code { get; set; }
        public DateTime create_time { get; set; }
    }
}
